package org.demo.nio.reactor.multiplethread;

import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.concurrent.*;

/**
 * Created by tangwy on 2017/10/31.
 */
public class Acceptor implements Runnable {
    private Selector selector;
    private ServerSocketChannel ssc;
    private ThreadPoolExecutor tpe=new ThreadPoolExecutor(4,4,10, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(10));
    public Acceptor(Selector selector, ServerSocketChannel ssc) {
        //判断是否为null
        this.selector = selector;
        this.ssc = ssc;
    }

    @Override
    public void run() {
        try {
            SocketChannel channel = ssc.accept();
            if(channel!=null){
                System.out.println("accept");
                tpe.submit(new RWHandler(this.selector,channel));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
